package cn.ylz.ylmall.account.dao.persist.repository.impl;

import cn.ylz.ylmall.account.dao.persist.mapper.RoleMapper;
import cn.ylz.ylmall.account.dao.persist.mapper.RolePermissionMapper;
import cn.ylz.ylmall.account.dao.persist.repository.IRoleRepository;
import cn.ylz.ylmall.account.pojo.entity.Role;
import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.pojo.vo.RoleListVO;
import cn.ylz.ylmall.account.pojo.vo.RoleStandardVO;
import cn.ylz.ylmall.account.pojo.vo.UserPermissionVO;
import cn.ylz.ylmall.common.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleRepositoryImpl implements IRoleRepository {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public int countByName(String name) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return roleMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public RoleStandardVO getStandardById(Long id) {
        return roleMapper.getStandardById(id);
    }

    @Override
    public int delete(Long id) {
        return roleMapper.deleteById(id);
    }

    @Override
    public int update(Role role) {
        return roleMapper.updateById(role);
    }

    @Override
    public PageData<RoleListVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<RoleListVO> list = roleMapper.list();
        PageInfo<RoleListVO> pageInfo = new PageInfo<>(list);
        PageData<RoleListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public List<UserPermissionVO> getPermissionByRoleId(Long roleId) {
        return rolePermissionMapper.getPermissionByRoleId(roleId);
    }
}
